/*
Project : Programmable Control of Airflow System for Maintaining Oxygen Saturation in Pre-term infant 
Microcontroller : STM32F4 Discovery (STM32F407VG)
File : task_oxygen_pulse_meter.c

Deverloper : Phattaradanai Kiratiwudhikul
Deverloped by Department of Electrical Engineering, Faculty of Engineering, Mahidol University

*/
//------------------------------------------------------------------------------
#include "FreeRTOS.h"
#include "freeRTOS_priority.h"

#include "semphr.h"
#include "queue.h"

#include "task_oxygen_pulse_meter.h"
#include "driver_oxygen_pulse_meter.h"
//------------------------------------------------------------------------------
xQueueHandle *OPM_Queue;
xSemaphoreHandle  OPM_Semaphore;

// Extern Variable -------------------------------------------------------------

//------------------------------------------------------------------------------
void start_oxygen_pulse_meter_task (void)
{
  oxygen_pulse_meter_setup();                                                   // Config driver for Oxygen pulse meter
  init_oxygen_pulse_meter_task();                                               // initial oxygen pulse meter task
}
//------------------------------------------------------------------------------
/*
  Function : oxygen_pulse_meter_setup
  Input : None
  Output : None
  Description : 
*/
void oxygen_pulse_meter_setup(void)
{
  oxygen_pulse_meter_driver_setup();                                            // setup usart for oxygen pulse meter
}
//------------------------------------------------------------------------------
/*
  Function : init_oxygen_pulse_meter_task
  Input : None 
  Output : uint8_t (return value)
  Description : Create Oxygen Pulse Meter Task, Create OPM_Queue for transfer data from ISR to Task
                Create Binary Semaphore : OPM_Semaphore
*/
uint8_t init_oxygen_pulse_meter_task(void)
{
  /* Create Queue for transfer Data of Oxygen pulse meter from ISR to oxygen_pulse_meter task */
  OPM_Queue = xQueueCreate(SIZE_OF_OPM_BUFFER, sizeof(char *));                 // Queue length 174 bytes
  if ( OPM_Queue == NULL )
  {
    /* OPM_Queue was not created */
    return (1);
  }
  
  /* Create OPM Binary Semaphore */
  vSemaphoreCreateBinary( OPM_Semaphore );
  if( OPM_Semaphore == NULL )
  {
    /* oxygen_pulse_meter_Semaphore was not created */
    return (1);
  }
  
  /* Create OPM (Oxygen Pulse Meter) Task */
  if(xTaskCreate(oxygen_pulse_meter_Task, "Oxygen Pulse Meter", configMINIMAL_STACK_SIZE, NULL, OPM_TASK_PRIORITY, NULL ) != pdPASS)
  {
    return(1);
  }
  return 0 ;
}
//------------------------------------------------------------------------------
void oxygen_pulse_meter_Task(void *pvParameter)
{ 
  portBASE_TYPE xQueue_Status;
  char cData_OPM[SIZE_OF_OPM_BUFFER];
  uint8_t uiCurrent_SpO2;
  
  SDecode_data_OPM SInformation_OPM;
  
  for(;;)
  {
    xSemaphoreTake( OPM_Semaphore, portMAX_DELAY );                             // Take Semaphore from ISR for unblocking oxygen_pulse_meter_Task
    
    xQueue_Status = xQueueReceive(OPM_Queue, &cData_OPM, 500/portTICK_RATE_MS);
    if (xQueue_Status == pdPASS)
    {
      SInformation_OPM = Get_OxygenSat(cData_OPM);                              // decode and get information from protocal
      
      if (SInformation_OPM.bReadCorrect == true)
      {
        /* case : read data correct */
      }
      else
      {
        /* case : read data incorrect*/
      }
    }
  }
}

// End of File -----------------------------------------------------------------
/*--------------------------------------------------------------------------------------------------
(C) Copyright 2014, Department of Electrical Engineering, Faculty of Engineering, Mahidol University
--------------------------------------------------------------------------------------------------*/

